package com.example.finallyjob.Controllers;

import com.example.finallyjob.service.ArticleMake;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;

@Api(description = "文章上传/视频上传/上传头像/修改作品")
@RestController
public class ArticleMakeController {
    @Autowired
    private ArticleMake articleMake;

    @ApiOperation(value = "将文章写入磁盘，文章信息加入数据库",notes = "返回文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "multipartFile", value = "文章文件", required = true, dataType = "__file"),
            @ApiImplicitParam(paramType = "form", name = "User_id", value = "用户id", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "form", name = "Artcle_id", value = "用户文章列表", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "kind", value = "作品类别（1/2/3/4）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "title", value = "作品名称", required = true, dataType = "String")
    })
    //将文章写入磁盘、数据库
    @PostMapping("/writefile")
    public String Makefile(MultipartFile file, String title,
                       int User_id, String Artcle_id, String kind) throws SQLException, IOException {
        return articleMake.file(file, title, User_id,
                Artcle_id, kind);

    }

    @ApiOperation(value = "上传头像",notes = "返回图片文件编号")
    //上传图片进磁盘
    @PostMapping("/WirterImage")
    public String WirterImage(MultipartFile myFile) throws IOException {
        return articleMake.WirterImage(myFile);
    }

    @ApiOperation(value = "上传文章图片",notes = "返回图片文件编号")
    //上传图片进磁盘
    @PostMapping("/WirterImg")
    public reImg WirterImage1(MultipartFile myFile) throws IOException {
        String name=articleMake.WirterImage(myFile);
        reImg p=new reImg();
        Img img=new Img();
        img.alt=name;
        img.url="/biology/openImage/"+name+".jpg";
        img.href="F:\\image\\"+name+".jpg";
        p.data[0]=img;
        p.errno=0;
        return p;
    }

    @ApiOperation(value = "上传视频",notes = "返回视频文件编号")
    //将视频写入磁盘
    @PostMapping("/WriteVideo")
    public String WriteVideo(MultipartFile myFile){
        return articleMake.WriteVideo(myFile);
    }

    @ApiOperation(value = "将视频写入磁盘，视频信息加入数据库",notes = "返回视频列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "num", value = "视频文件编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "User_id", value = "用户id", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "form", name = "Artcle_id", value = "用户视频列表", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "kind", value = "作品类别（1/2/3/4）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "title", value = "作品名称", required = true, dataType = "String")
    })
    //将视频作品信息写入数据库
    @PostMapping("/insertVideo")
    public String insertVideo( int User_id, String Artcle_id,
                              String title, String num, String kind){
        return articleMake.insertVideo(User_id, Artcle_id,
                title, num, kind);
    }

    //修改作品内容
    @ApiOperation(value = "修改作品内容")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "file", value = "文章文件", required = false, dataType = "__file"),
            @ApiImplicitParam(paramType = "form", name = "num", value = "视频文件编号", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "article_id", value = "作品id", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "form", name = "type", value = "作品类别（text||video）", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "form", name = "title", value = "作品名称", required = false, dataType = "String")
    })
    @PutMapping("/UpdateArticle")
    public Boolean UpdateArticle(MultipartFile file,String num ,
                                 String title,int article_id,String type) throws IOException {
        return articleMake.UpdateArticle(file, num, title,
                article_id, type);
    }

    class reImg implements Serializable {
        public int errno=0;
        public Img[] data=new Img[10];

    }
    class Img implements Serializable {
        public String url;
        public String alt;
        public String href;
    }
}
